package com.embhealth.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.embhealth.constant.RedisConstant;
import com.embhealth.dao.SetmealDao;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.entity.Result;
import com.embhealth.pojo.Setmeal;
import com.embhealth.service.SetmealService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService{
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    /**
     * 新增套餐
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        this.setSetmealAndCheckgroup(setmealId,checkgroupIds);
        //将图片名称保存到Redis中
        String fileName = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);
    }

    /**
     * 分页查询
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page=setmealDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询套餐信息，用于编辑业务的数据回显
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }
    //查询套餐关联的所有检查组id
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        return setmealDao.findCheckGroupIdsBySetmealId(id);
    }
    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        //修改检查组基本信息
        setmealDao.edit(setmeal);
        //清理当前检查组关联的检查项，操作中间关系表
        setmealDao.deleteAssocication(setmeal.getId());
        //重新建立关联关系
        setSetmealAndCheckgroup(setmeal.getId(),checkgroupIds);
    }
    @Override
    public void delete(Integer id){
        //删除关联关系(先删除)
        setmealDao.deleteAssocication(id);
        //删除本表数据
        setmealDao.delete(id);
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setmealDao.findSetmealCount();
    }
    //套餐收入统计
    @Override
    public Map<String, Object> findSetmealIncome() {
        Map<String,Object> map=new HashMap<>();
        //调用dao层获取每个套餐的收入
        List<Map<String,Object>> setMealIncome=setmealDao.findSetmealIncomeCount();

        //调用dao层获取总收入
        Integer totalIncome=setmealDao.totalIncome();
        //创建map集合封装总收入
        Map<String,Object> map2=new HashMap<>();
        map2.put("value",totalIncome);//总收入的值
        map2.put("name","总收入");//总收入名字
        setMealIncome.add(map2);//将总收入相关信息封装到list中
        map.put("setMealIncome",setMealIncome);

        //套餐名字集合
        List<String>setMealNames=new ArrayList<>();
        for (Map<String, Object> stMap : setMealIncome) {
            String  name = (String) stMap.get("name");
            setMealNames.add(name);
            map.put("setMealNames",setMealNames);
        }
        System.out.println(map);
        return map;
    }

    /**
     * 关联检查组方法
     */
    public void setSetmealAndCheckgroup(Integer setmealId,Integer[] checkgroupIds){
        if(checkgroupIds != null && checkgroupIds.length > 0){
            for (Integer checkgroupId : checkgroupIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }
}
